#include "EditTestCaseMain.h"
#include<qmenu.h>
#include <QMessageBox>
#include<TestCaseAdd.h>
#include <EditProcedure.h>
#include<QFile>
#include"projectInfo.h"
#include <QFileDialog>
#include <QString>
#include <QtXml>
#include "Tool/excelhandler.h"
EditTestCase::EditTestCase(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	InitUI();

//	AddTreeWidgetData();
	ConnectAllSlots();
}

EditTestCase::~EditTestCase()
{
    qDebug() << __FUNCTION__;
}

void EditTestCase::ConnectAllSlots()
{
	connect(ui.treeWidget, &QTreeWidget::itemClicked, this, &EditTestCase::OnTreeWidgetClicked);
	connect(ui.pushButton_createPro, &QPushButton::clicked, this, &EditTestCase::OnPushbutton_CreateProClicked);
	connect(ui.pushButton_openPro, &QPushButton::clicked, this, &EditTestCase::OnPushbutton_OpenPro_Clicked);
	connect(ui.pushButton_deletePro, &QPushButton::clicked, this, &EditTestCase::OnPushbutton_DeletePro_Clicked);

	connect(projectInfoEdit, &projectInfo::signalCreate, this, &EditTestCase::OnSinalCreate);
}

void EditTestCase::InitUI()
{

	projectInfoEdit = new projectInfo();
	procedureEdit = new EditProcedure();

	CreateTreeWidgetMenu();
    ui.treeWidget->header()->hide();    //隐藏表头
	//树控件模式设定
	ui.treeWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为，以行为单位
	ui.treeWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置选择模式，选择单行
	ui.treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

}


void EditTestCase::AddTreeWidgetData()
{
	//设置表头
	//QStringList topTitle;
	//topTitle << "0" << "1" << "2" << "3" << "4";
	//ui.treeWidget->setHeaderLabels(topTitle);
	ui.treeWidget->header()->hide();    //隐藏表头
	//添加一级节点
	QStringList data1(QString::fromLocal8Bit("测试用例1"));
	QTreeWidgetItem* treeWidgetItem_1 = new QTreeWidgetItem(data1);
	ui.treeWidget->addTopLevelItem(treeWidgetItem_1);

	//添加子节点
	QStringList childdata1(QString::fromLocal8Bit("测试步骤1"));
	QTreeWidgetItem* childItem = new QTreeWidgetItem(childdata1);
	treeWidgetItem_1->addChild(childItem);


	ui.treeWidget->expandAll();

}

void EditTestCase::CreateTreeWidgetMenu()
{

	ui.treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);//右键 不可少否则右键无反应
	connect(ui.treeWidget, &QTreeWidget::customContextMenuRequested, this, &EditTestCase::slots_showrightMenu);
	popMenu = new QMenu(this);
    addNode = new QAction(QString::fromLocal8Bit("添加测试项目"), this);
    addNode->setStatusTip(QString::fromLocal8Bit("添加测试项目"));
    deleteNode = new QAction(QString::fromLocal8Bit("删除项目"), this);
    deleteNode->setStatusTip(QString::fromLocal8Bit("删除项目"));

	connect(addNode, &QAction::triggered, this, &EditTestCase::createTestCase);
	connect(deleteNode, &QAction::triggered, this, &EditTestCase::delTestCase);
}

void EditTestCase::createTestCase()
{
	TestCaseAdd testAddForm;
	int result = testAddForm.exec();
	if (result)
	{
		QStringList childdata1(testAddForm.InputValue());
		QTreeWidgetItem* childItem = new QTreeWidgetItem(childdata1);
        if(selectItem->parent()==nullptr)
        {
            selectItem->addChild(childItem);
            QString strPro = selectItem->text(0);
            qDebug()<<strPro;
        }
        else
        {
            selectItem->parent()->addChild(childItem);

            QString strPro = selectItem->parent()->text(0);
            qDebug()<<strPro;
        }
        ui.treeWidget->expandAll();

	}

}

void EditTestCase::delTestCase()
{

}

/**
 * @brief EditCmdWindow::slots_showrightMenu    在列表上右键显示菜单
 * @param point
 */
void EditTestCase::slots_showrightMenu(QPoint point)
{
	popMenu->clear();//清除原有菜单
	popMenu->addAction(addNode);
	popMenu->addAction(deleteNode);
	selectItem = ui.treeWidget->itemAt(point); //可得到右键条目

	popMenu->exec(QCursor::pos());
}

void EditTestCase::OnTreeWidgetClicked()
{
	//QMessageBox::warning(nullptr,"", ui.treeWidget->currentItem()->text(0));
	bool bTopItem = ui.treeWidget->currentItem()->parent() == NULL;
	if (bTopItem)
	{
		if (projectInfoEdit == nullptr)
		{
			projectInfoEdit = new projectInfo();
		}
        ui.stackedWidget->addWidget(projectInfoEdit);
		ui.stackedWidget->setCurrentWidget(projectInfoEdit);

		//todo:加载显示相应内容
	}
	else
	{
		if (procedureEdit == nullptr)
		{
			procedureEdit = new EditProcedure();
		}

        ui.stackedWidget->addWidget(procedureEdit);
		ui.stackedWidget->setCurrentWidget(procedureEdit);
		//todo:加载显示相应内容
        QString strTestCase= ui.treeWidget->currentItem()->text(0);
        procedureEdit->SetTestCaseName(strTestCase);
	}



}


void EditTestCase::SaveTestCase()
{
	//nodeConfigInfo
}

void EditTestCase::OnPushbutton_CreateProClicked()
{
	if (projectInfoEdit == nullptr)
	{
		projectInfoEdit = new projectInfo();
	}
	ui.stackedWidget->addWidget(projectInfoEdit);
	ui.stackedWidget->setCurrentWidget(projectInfoEdit);

	//添加内容
	//QStringList data1(QString::fromLocal8Bit("测试用例1"));
	//QTreeWidgetItem* treeWidgetItem_1 = new QTreeWidgetItem(data1);
	//ui.treeWidget->addTopLevelItem(treeWidgetItem_1);


	//内容缓存

}

void EditTestCase::OnPushbutton_OpenPro_Clicked()
{
}

void EditTestCase::OnPushbutton_DeletePro_Clicked()
{
}

void EditTestCase::OnSinalCreate()
{

	//检测是否有同名头
	QString strPro = projectInfoEdit->getProjectName();
	QString strPeople = projectInfoEdit->getProjectName();
	QString strProtocalPath = projectInfoEdit->getProtocalPath();
	QDateTime date = projectInfoEdit->getDateTime();

    if (_tcmsEditInfos.contains(strPro) || strPro.isEmpty() || strProtocalPath.isEmpty())
	{
		QString strInfo;
        if (_tcmsEditInfos.contains(strPro))
		{
			strInfo += QString::fromLocal8Bit("已有同名项目项目名，");
		}
		if (strPro.isEmpty())
		{
			strInfo += QString::fromLocal8Bit("项目名为空，");
		}
        if (strProtocalPath.isEmpty())
        {
            strInfo += QString::fromLocal8Bit("协议路径为空,");
        }

		strInfo += QString::fromLocal8Bit("创建失败");
		QMessageBox::information(this, QString::fromLocal8Bit("提示信息"), strInfo);

		return;
	}
	//添加创建信息

	QStringList data1(strPro);
	QTreeWidgetItem* treeWidgetItem_1 = new QTreeWidgetItem(data1);
	ui.treeWidget->addTopLevelItem(treeWidgetItem_1);


	//添加内容到缓存
	ProjectInfo proinfo;
	proinfo.SetProInfo(strProtocalPath, strPeople, strPro, date);
    _tcmsEditInfos[strPro]._projectinfo = proinfo;

}

void EditTestCase::on_pushButton_saveAsXml_clicked()
{
    //保存编辑信息，用于下次加载
    //树信息+保存excel文件名
}

void EditTestCase::on_pushButton_savePro_clicked()
{
    QString fileName;
    //打开当前程序所在路径
    QDir Appdir(QApplication::applicationDirPath());
    QString directory = QDir::currentPath(); // 获取当前目录作为默认目录
    // 创建 QFileDialog 实例并设置对话框属性
    QFileDialog dialog(nullptr, QString::fromLocal8Bit("选择文件夹并保存文件"), directory, "xlsx Files (*.xlsx);;All Files (*)");
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setAcceptMode(QFileDialog::AcceptSave);

    // 显示对话框并获取用户选择的文件路径
    if (dialog.exec()) {
        fileName = dialog.selectedFiles().first();
        // 在这里处理文件保存逻辑
        qDebug() <<QString::fromLocal8Bit( "选择的文件路径：" )<< fileName;
    }

    procedureEdit->SaveModifyResultToExcel(fileName);
}
